<?php
use yii\bootstrap\ActiveForm;
use yii\helpers\Url;
use yii\widgets\LinkPager;
use yii\widgets\Pjax;

$request = Yii::$app->getRequest();
$member_id = $request->get('id', 0);
?>

<style>
	input[type="checkbox"] {
		display: block;
	}

	input[name="key"]{padding-left: 35px;background:url(<?=Yii::getAlias('@web/backend/img/common/search.png');?>) no-repeat 6px center;}
</style>

<div class="clearfix mt20">
	<div class="pull-left lh40" style="margin-left: 25px;">疗程卡名称</div>
	<div class="pull-right">
		<form action="<?= Url::to(['beautycard']) ?>">
			<input type="hidden" name="id" value="<?= $member_id ?>">
			<input class="lh40" type="text" name="key" value="<?= $request->get('key') ?>" placeholder="疗程卡名称">
			<a  class="btnBg dis text-center mg20-x" style=" height: 40px;">
				<input  type="submit" value="搜索"  style="display: block; width: 100%; background: 0; border:0;line-height: 40px;">
			</a>
		</form>
	</div>
</div>
<div id="basic" class="container" style="padding: 0 130px">


	<?php ActiveForm::begin();?>

	<?php Pjax::begin() ?>
	<?php foreach($beautyCardList as $beautyCard): ?>
	<div class="checkbox">
		<div offset="<?= $beautyCard['config_id'] ?>" total-count="<?= $beautyCard['config_val'] ?>" beauty-card="<?= $beautyCard['config_key'] ?>" total_price="<?= $beautyCard['config_val2'] ?>" style="width: 100%; margin-top: 25px;">
			<input type="checkbox" onchange="addInput(this)" <?= $beautyCard['beautycard_id'] ? 'checked' : '' ?> ><?= $beautyCard['config_key'] ?> ( <?= $beautyCard['config_val'] ?> )

			<?php if ($beautyCard['beautycard_id']): ?>
				<span class="input-remove pull-right ml20">&nbsp;&nbsp;&nbsp; 金额  <input type="text" name="total_price_<?= $beautyCard['config_id'] ?>" value="<?= $beautyCard['total_price'] ?>" onblur="setAvailableCount(<?= $beautyCard['config_id'] ?>, <?= $member_id ?>, <?= $beautyCard['config_val'] ?>)" style="width: 50px; color: #fe7b9d;"></span>
				<span class="input-remove pull-right">&nbsp;&nbsp;&nbsp; 可用次数  <input type="text" name="available_count_<?= $beautyCard['config_id'] ?>" value="<?= $beautyCard['available_count'] ?>" onblur="setAvailableCount(<?= $beautyCard['config_id'] ?>, <?= $member_id ?>, <?= $beautyCard['config_val'] ?>)" style=" color: #fe7b9d;width: 100px;"></span>

			<?php endif; ?>
		</div>
	</div>
	<?php endforeach; ?>

	<div class="page">
		<?= LinkPager::widget([
			'pagination' => $page,
			// 'hideOnSinglePage' => true
		]); ?>
	</div>
	<?php Pjax::end() ?>

	<hr/>

	<div class="checked">
	<!--已选择的疗程卡-->
		<?php foreach($beautyCardListAll as $beautyCard): ?>
			<?php if ($beautyCard['beautycard_id']){ ?>
				<span class="cards-list offset<?= $beautyCard['config_id'] ?>"><?= $beautyCard['config_key'] ?>&nbsp;&nbsp;</span>
			<?php } ?>
		<?php endforeach; ?>
	</div>

	<div class="text-center" style=" margin-top: 20px; margin-bottom: 30px;" >
		<input type="hidden" name="member_id" value="<?= $member_id ?>"/>
		<span class="btnMd mr20"><input type="submit" value="确定" style="display: inline-block; width: 100%; background: 0;line-height: 25px; border: 0"  /></span>
		<span class="btnMd bg_rest "><input type="submit" value="取消" style="display: inline-block; width: 100%; background: 0;line-height: 25px; border: 0"  /></span>
	</div>

	<?php ActiveForm::end() ?>

</div>

<script>
<?php $this->beginBlock('js_index') ?>
$('input[type="reset"]').click(function(){
	var index = parent.layer.getFrameIndex(window.name);
	parent.layer.close(index);
});

/**
 * 增加次数输入框
 * @param object
 */
function addInput(object) {
	var container = $(object).parent();
	var checked = $('.checked');
	var offset = container.attr('offset');
	var member_id = <?= $member_id ?>;
	var total_count = container.attr('total-count');
	var beauty_card = container.attr('beauty-card');
	var total_price = $('input[name=total_price_'+ offset +']').val();
	if (!total_price) {
		total_price = '';
	}

	if (object.checked == true) {
		$.get("<?= Url::to(['addbeautycard']) ?>", {beautycard_id:offset, member_id:member_id, total_count:total_count, beauty_card:beauty_card, total_price:total_price}, function(status) {
			if (status == 1) {
				container.append('<span class="input-remove pull-right ml20">&nbsp;&nbsp;&nbsp; 金额  <input type="text" name="total_price_'+ offset +'" value="'+ total_price +'" onblur="setAvailableCount('  + offset +', ' + member_id + ',' + total_count + ')" style="width: 50px; color: #fe7b9d;"></span><span class="input-remove pull-right">&nbsp;&nbsp;&nbsp; 可用次数  <input type="text" name="available_count_'+ offset +'" style=" color: #fe7b9d;width: 100px;" onblur="setAvailableCount('  + offset +', ' + member_id + ',' + total_count + ')"></span>');

				checked.append('<span class="cards-list offset' + offset + '">' + beauty_card + '&nbsp;&nbsp;</span>');
			}
		});
	} else {
		$.get("<?= Url::to(['addbeautycard', 'type' => false]) ?>", {beautycard_id:offset, member_id:member_id, beauty_card:beauty_card}, function(status) {
			if (status == 1) {
				container.find(".input-remove").remove();
				checked.find('.offset' + offset).remove();
			}
		});
	}
}

/**
 * 修改疗程卡的可用次数
 */
function setAvailableCount(beautycard_id, member_id, total_count)
{
	var object = $('input[name=available_count_'+ beautycard_id +']');
	var obj_total_price = $('input[name=total_price_'+ beautycard_id +']');
	var available_count = object.val();
	var status = true;

	if (!/^\d*$/.test(available_count)) {
		status = false;
		object.val('');
		layer.alert('只能输入数字',{icon:2});
	}
	if (available_count > total_count) {
		status = false;
		object.val('');
		layer.alert('可用次数不能大于总次数',{icon:2});
	}
 	if (status == true) {
		var total_price = obj_total_price.val();
		$.get("<?= Url::to(['setbeautycard']) ?>", {beautycard_id:beautycard_id, member_id:member_id, available_count:available_count ? available_count : 0, total_price:total_price}, function(status) {

		});
	}
}

<?php $this->endBlock() ?>
<?php $this->registerJs($this->blocks['js_index'], $this::POS_END); ?>
</script>